﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using MeTalker.Model;
using System.Data;
using MeTalker.DBUtility;

namespace MeTalker.DAL
{
    public class LoginLogDAL : DALBase
    {
        public static int DeleteAll()
        {
            try
            {
                string sql = "delete from LoginLog";
                return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, null);
            }
            catch
            {
                return 0;
            }
        }

        public static int Delete(string autoId)
        {
            try
            {
                string sql = "delete from LoginLog where AutoID=@AutoID";
                SqlParameter paramAutoID = new SqlParameter("@AutoID", SqlDbType.Int, 1, "AutoID");
                paramAutoID.Value = int.Parse(autoId);
                return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, new SqlParameter[] { paramAutoID });
            }
            catch
            {
                return 0;
            }
        }
        public static DataTable GetLogsFilter(string filter){
            string sql = "select * from LoginLog";
            if (!string.IsNullOrEmpty(filter)) {
                sql += " where " + filter;
            }
            return SqlHelper.ExecuteDataTable(sql);
        }
        public static DataTable GetLogs()
        {
            try
            {
                string sql = "select * from LoginLog";
                return SqlHelper.ExecuteDataTable(sql);
            }
            catch
            {
                return null;
            }
        }

        public static int Insert(LoginLogMod login)
        {
            try
            {
                string sql = "insert into LoginLog(UserName,SoftName,LoginDate,IpAddress) values(@UserName,@SoftName,@LoginDate,@IpAddress)";
                return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, GetSqlParameter(login));
            }
            catch
            {
                return 0;
            }
        }

        static SqlParameter[] GetSqlParameter(LoginLogMod login)
        {
            List<SqlParameter> paramss = new List<SqlParameter>();
            SqlParameter paramAutoID = new SqlParameter("@AutoID", SqlDbType.Int, 4, "AutoID");
            paramAutoID.Value = login.AutoID;
            paramss.Add(paramAutoID);

            SqlParameter paramUserName = new SqlParameter("@UserName", SqlDbType.VarChar, 100, "UserName");
            paramUserName.Value = login.UserName;
            paramss.Add(paramUserName);

            SqlParameter paramSoftName = new SqlParameter("@SoftName", SqlDbType.VarChar, 100, "SoftName");
            paramSoftName.Value = login.SoftName;
            paramss.Add(paramSoftName);

            SqlParameter paramLoginDate = new SqlParameter("@LoginDate", SqlDbType.DateTime, 8, "LoginDate");
            paramLoginDate.Value = login.LoginDate;
            paramss.Add(paramLoginDate);


            SqlParameter paramIpAddress = new SqlParameter("@IpAddress", SqlDbType.VarChar, 100, "IpAddress");
            paramIpAddress.Value = login.IpAddress;
            paramss.Add(paramIpAddress);
            return paramss.ToArray();
        }
    }
}
